﻿using ChartManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChartManager.CustomControls
{
    /// <summary>
    /// 选择按钮
    /// </summary>
    class SelectButton : TextButton
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="text">按钮文本</param>
        /// <param name="textbox">关联输入框</param>
        /// <param name="context">EFContext</param>
        /// <param name="mode">选择模式</param>
        public SelectButton(string text, SongTextBox textbox, EFContext context, SelectMode mode) : base(text)
        {
            Click += SelectButton_Click;
            Textbox = textbox;
            Context = context;
            Mode = mode;
        }

        private SongTextBox Textbox { get; set; }
        private EFContext Context { get; set; }
        private SelectMode Mode { get; set; }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            // 点击时打开Form2
            var formSelect = new Form2(Context, Mode);
            try
            {
                // 并获取其对话框结果
                if (formSelect.ShowDialog() == DialogResult.OK)
                {
                    // 将选择结果赋值到关联输入框中
                    Textbox.song = formSelect.Result;
                }
            }
            // Form2遇到没有歌曲时提前关闭抛出空引用异常
            catch (ObjectDisposedException) { }
        }
    }
}
